const app = getApp()

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    praisenum:0,
    img:null,
    content:'',
    user:'',
    praise:'',
    list:[],
    listr:[],
    list_item:[],  
    praisenum:1,
    stype:''
  },




//切换不同类型的游记  
  showType: function(e){
    console.log(e.target.id)
    var that = this
    var praisenumn = 0
    that.setData({
      stype : e.target.id
    }) 
    console.log(that.data.stype)
    if (e.target.id != "全部"){
      
    
    wx: wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pager+'&size=1',
      data: {
        stype: that.data.stype
      },
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(res)
        if (res.data.msg == "获取游记列表为空"){
          wx.showToast({
            title: '无相关视游记',
            icon: 'none',
            duration: 1500
          })
        }
        that.setData({
          listr: res.data.data.data,
           
        })  
         
      },
      fail: function (res) { },
      complete: function (res) { },
    })

    wx: wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pagel + '&size=1',
      data: {
        stype: that.data.stype
      },
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {

        console.log(res)
        that.setData({
          list: res.data.data.data,

        })

      },
      fail: function (res) { },
      complete: function (res) { },
    })

  }else{
      console.log(app.globalData.pager)
      console.log(app.globalData.pagel)
      var that = this
      wx: wx.request({
        url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pager + "&size=" + 2,
        data: {
        },
        header: {
        },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res.data.data.data)
          that.setData({
            list: res.data.data.data
          })
          console.log(that.data.list)

        },
        fail: function (res) {
          console.log(res)
        },
        complete: function (res) { },
      })
      wx: wx.request({
        url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pagel + "&size=" + 2,
        data: {},
        header: {},
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res.data.data.data)
          that.setData({
            listr: res.data.data.data
          })
        },
        fail: function (res) {
          console.log(res)
        },
        complete: function (res) { },
      })
  }


    //console.log(that.data.list)
  },


  //打开对应id的详细游记内容
  content_item: function(e) {
    var that = this
    var content_list
    var sid = e.currentTarget.id
    console.log("游记id为："+sid)
    if (app.globalData.alltoken != null){
      wx: wx.request({
        url: app.globalData.url + '/v1/Strategy/getStrategyDetail?sid='+sid,
      data: '',
      header: {
        "token": app.globalData.alltoken
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          list_item: res.data.data
        })
        app.globalData.content_item = that.data.list_item
        var item = JSON.stringify(app.globalData.content_item);
       // console.log("转换游记内容:" + item)
        wx.navigateTo({
          url: '../content/content'
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    }
    else{
      wx:wx.showToast({
        title: '请先登录',
        icon: 'none',
     
      })
    }
  },


  //搜索
  goSearch: function (e) {
    var that = this;
    var formData = e.detail.value;
    console.log(formData);
    if (formData) {
      wx.request({
        url: app.globalData.url + '/v1/Strategy/listStrategy',
        data: {
          title: formData
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res)

          if (res.data.length == 0 ) {
            wx.showToast({
              title: '无相关视游记',
              icon: 'none',
              duration: 1500
            })
          } else {
            wx: wx.request({
              url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pager + '&size=1',
              data: {
                stype: formData
              },
              header: {},
              method: 'POST',
              dataType: 'json',
              responseType: 'text',
              success: function (res) {
                console.log(res)
                if (res.data.msg == "获取游记列表为空") {
                  wx.showToast({
                    title: '无相关视游记',
                    icon: 'none',
                    duration: 1500
                  })
                }
                that.setData({
                  listr: res.data.data.data,

                })

              },
              fail: function (res) { },
              complete: function (res) { },
            })

            wx: wx.request({
              url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pagel + '&size=1',
              data: {
                stype: formData
              },
              header: {},
              method: 'POST',
              dataType: 'json',
              responseType: 'text',
              success: function (res) {

                console.log(res)
                that.setData({
                  list: res.data.data.data,

                })

              },
              fail: function (res) { },
              complete: function (res) { },
            })
          }

          // console.log(res.data.msg)
        }
      })
    } else {

      wx.showToast({
        title: '输入不能为空',
        icon: 'none',
        duration: 1500
      })

    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.pager)
    console.log(app.globalData.pagel)  
    var that = this
    wx: wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pager +"&size=" + 2,
      data: {
      },
      header: {
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log( res.data.data.data)
        that.setData({
          list: res.data.data.data
        })
      console.log(that.data.list)

      },
      fail: function (res) { 
        console.log(res)
      },
      complete: function (res) { },
    })
    wx: wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + app.globalData.pagel + "&size=" + 2,
      data: {},
      header: {},
      method: 'GET',
      dataType: 'json',   
      responseType: 'text',
      success: function (res) {
        console.log( res.data.data.data)
        that.setData({
          listr: res.data.data.data
        })
      },
      fail: function (res) {
        console.log(res)
       },
      complete: function (res) { },
    })

  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })

if(that.data.stype == ''){




    // 页数+1
    var pagert = app.globalData.pager + 2;
    //console.log(pagert)
    wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + pagert + '&size=2',
      method: "GET",
      // 请求头部
      header: {
        'content-type': 'application/text'
      },
      success: function (res) {
        // 回调函数
        console.log(res.statusCode)
        var listr = that.data.listr;
        if(res.statusCode == 404){
          wx:wx.showToast({
            title: '无更多游记',
            icon: 'none',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }else{
        for (var i = 0; i < res.data.data.data.length; i++) {
          listr.push(res.data.data.data[i]);
        }
        // 设置数据
        that.setData({
          listr: that.data.listr,
        })
        // 隐藏加载框
        wx.hideLoading();

        app.globalData.pager = pagert
        console.log(app.globalData.pager)
      }
      }
    })

    var pagelt = app.globalData.pagel + 2;
    //console.log(pagelt)
    wx.request({
      url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + pagelt + '&size=2',
      method: "GET",
      // 请求头部
      header: {
        'content-type': 'application/text'
      },
      success: function (res) {
        // 回调函数
        console.log(res.statusCode)
        var list = that.data.list;
        if (res.statusCode == 404) {
          wx: wx.showToast({
            title: '无更多游记',
            icon: 'none',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
        for (var i = 0; i < res.data.data.data.length; i++) {
          list.push(res.data.data.data[i]);
        }
        // 设置数据
        that.setData({
          list: that.data.list,
        })
        // 隐藏加载框
        wx.hideLoading();

        app.globalData.pagel = pagelt
        console.log(app.globalData.pagel)
      }
      }
    })

  }else{
  console.log(that.data.stype)
  // 页数+1
  var pagert = app.globalData.pager + 2;
  //console.log(pagert)
  wx.request({
    url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + pagert + '&size=2',
    data: {
      stype: that.data.stype
    },
    method: "GET",
    // 请求头部
    header: {
      'content-type': 'application/text'
    },
    success: function (res) {
      // 回调函数
      console.log(res.statusCode)
      var listr = that.data.listr;
      if (res.statusCode == 404) {
        wx: wx.showToast({
          title: '无更多游记',
          icon: 'none',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else {
        for (var i = 0; i < res.data.data.data.length; i++) {
          listr.push(res.data.data.data[i]);
        }
        // 设置数据
        that.setData({
          listr: that.data.listr,
        })
        // 隐藏加载框
        wx.hideLoading();

        app.globalData.pager = pagert
        console.log(app.globalData.pager)
      }
    }
  })

  var pagelt = app.globalData.pagel + 2;
  //console.log(pagelt)
  wx.request({
    url: app.globalData.url + '/v1/Strategy/listStrategy?page=' + pagelt + '&size=2',
    data: {
      stype: that.data.stype
    },
    method: "GET",
    // 请求头部
    header: {
      'content-type': 'application/text'
    },
    success: function (res) {
      // 回调函数
      console.log(res.statusCode)
      var list = that.data.list;
      if (res.statusCode == 404) {
        wx: wx.showToast({
          title: '无更多游记',
          icon: 'none',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      } else {
        for (var i = 0; i < res.data.data.data.length; i++) {
          list.push(res.data.data.data[i]);
        }
        // 设置数据
        that.setData({
          list: that.data.list,
        })
        // 隐藏加载框
        wx.hideLoading();

        app.globalData.pagel = pagelt
        console.log(app.globalData.pagel)
      }
    }
  })





  }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})